package com.example.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.backend.common.ResultMessage;
import com.example.backend.entity.ShouCang;
import com.example.backend.service.IShouCangService;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j;
import org.springframework.web.bind.annotation.*;

@Log4j
@RestController
@RequestMapping("/shoucang")
public class ShouCangController {

    @Resource
    private IShouCangService shouCangService;

    @PostMapping
    public ResultMessage addShouCang(@RequestBody ShouCang shouCang){
        boolean flag =  shouCangService.addShouCang(shouCang);
        if (flag){
            return ResultMessage.success(true,"收藏成功");
        }
        return ResultMessage.error("收藏失败");
    }

    @DeleteMapping("/{scid}")
    public ResultMessage deleteShouCang(@PathVariable("scid") Integer scid){
        boolean flag = shouCangService.deleteShouCang(scid);
        if (flag){
            return ResultMessage.success(true,"删除收藏成功");
        }
        return ResultMessage.error("删除收藏失败");
    }

    @PutMapping
    public ResultMessage updateShouCang(@RequestBody ShouCang shouCang){
        boolean flag = shouCangService.updateShouCang(shouCang);
        if (flag){
            return ResultMessage.success(true,"更新收藏成功");
        }
        return ResultMessage.error("更新收藏失败");
    }

    @GetMapping
    public ResultMessage selectShouCang(ShouCang shouCang,
                                        @RequestParam(defaultValue = "1") Integer currentPage,
                                        @RequestParam(defaultValue = "5") Integer pageSize,
                                        @RequestParam(required = false) String sortField,
                                        @RequestParam(required = false) String sortOrder){
        IPage<ShouCang> shouCangList = shouCangService.selectShouCang(shouCang,currentPage,pageSize,sortField,sortOrder);
        if (shouCangList != null){
            return ResultMessage.success(shouCangList,"查询收藏成功");
        }
        return ResultMessage.error("查询收藏失败");
    }

    @GetMapping("/yhid/{yhid}/{sjid}")
    public ResultMessage findSc(@PathVariable("yhid") Integer yhid,
                                @PathVariable("sjid") Integer sjid){
        boolean flag = shouCangService.findSc(yhid,sjid);
        if (flag){
            return ResultMessage.success(true,"已收藏");
        }
        return ResultMessage.error("未收藏");
    }
}
